package com.intelligentmeterreadingsys.checkwork.service;


import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;


import android.app.PendingIntent;
import android.app.PendingIntent.CanceledException;
import android.app.Service;
import android.content.Intent;

import android.net.Uri;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.provider.Settings;
import android.text.TextUtils;
import android.widget.Toast;

/**
 * 后台自动定位服务类
 * @author king
 *
 */
public class GpsService extends Service {
	
     private LocationClient client=null;//定位类
     
   //时间间隔
 	private static int myTime = 15*1000 ;
 
 	private String mData;  //获取的数据
 	//定位时间间隔
 	private int myLocationTime = 5*1000; 
 	//是否启动了定位API
 	private boolean isOpenLocation = false;
 	//是否启动了定位线程
 	private boolean isOpenLocationTask = true;
 	//经纬度
 	private double Latitude = 0.0;
 	private double Longitude=0.0;
 	
 	
 	
 	private BDLocationListener listener=null;
 	

	//定时器
	private Timer myLocationTimer = null;
	//定时线程
	private  TimerTask myLocationTimerTask = null;
	
    private boolean islocation=false;//定位成功标志
	
	public long time;

	
	@Override
	public IBinder onBind(Intent intent) {
		
		return null;
	}

	@Override
	public void onCreate() {
		System.out.println("启动服务");
		System.out.println(this.getMainLooper().getThread().getId()+"main");
		time=System.currentTimeMillis();
		if(!isGPSEnable()){
			OpenGps();
		}
		start();
		
	client.start();
		super.onCreate();
	}

	@Override
	public void onDestroy() {
	   if(client!=null&&client.isStarted()){
		   client.stop();
	   }
		super.onDestroy();
	}
	
	

	/**
	 * 打开定位
	 */
	public void start(){
		
		
		if(!isOpenLocation){
			System.out.println("初始化定位");
			 client=new LocationClient(this);
			 LocationClientOption option = new LocationClientOption();
		        //打开GPS
		        option.setOpenGps(true);
		        //设置坐标类型
		        option.setCoorType("bd09ll");	
		        
		        //设置网络优先
	           option.setPriority(LocationClientOption.GpsFirst);	
		        //option.setPriority(LocationClientOption.NetWorkFirst);
		        
		        //设置产品线名称
		        option.setProdName("waterSys");
		        
		        //设置定位定时时间
		        option.setScanSpan(10000);	
		        client.setLocOption(option);
		        
		        client.registerLocationListener(new BDLocationListener() {
					
					@Override
					public void onReceivePoi(BDLocation arg0) {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void onReceiveLocation(BDLocation loction) {
						System.out.println(loction.getLocType());
						OpenGps();
						getLocationInfo(loction);
						
					}
				});
		       
		}
	}
	
	
	

	/**
	 * 得到定位信息
	 * @param location
	 */
	public void getLocationInfo(BDLocation location){
		 System.out.println(new Date());
		 if(location!=null&&location.getLocType()!=167){
			 this.Latitude=location.getLatitude();
			 this.Longitude=location.getLongitude();
			 this.islocation=true;
		 }
	}
    
	
	/**
	 * 打开关闭GPS
	 */
 public void OpenGps(){
	 Intent GPSIntent = new Intent();
	 GPSIntent.setClassName("com.android.settings",
	         "com.android.settings.widget.SettingsAppWidgetProvider");
	 GPSIntent.addCategory("android.intent.category.ALTERNATIVE");
	 GPSIntent.setData(Uri.parse("custom:3"));
	 try {
	     PendingIntent.getBroadcast(this, 0, GPSIntent, 0).send();
	 } catch (CanceledException e) {
	     e.printStackTrace();
	 }    
 }
 
 /**
  * 判断GPS是否打开
  * @return
  */
 public boolean isGPSEnable() {
		
		String str = Settings.Secure.getString(getContentResolver(),
				Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
		
		if (str != null) {
			System.out.println(str);
			return str.contains("gps");
		}
		else{
			return false;
		}
	}
	
}
